﻿using MODEL;
using SysHelper;
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SysCommon;
using System.Web.Mvc;
using System.IO;
using System.Web;
using MODEL.View.Products;
using SysHelper.Attributes;

namespace SysController.Products
{
    [SkipPemission]
    public class ProductController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        { 
            return View();
        }

        [HttpPost]
        [AjaxRequest]
        public ActionResult Index(ProductRequest request)
        {
            Expression<Func<V_ProductRespones, bool>> wheres = s => true;
            if (!string.IsNullOrWhiteSpace(request.Code))
            {
                string[] codes = request.Code.Replace('，', ',').Trim(',').Split(',');
                wheres = wheres.And(s => codes.Contains(s.Code));
            }
            if (!string.IsNullOrWhiteSpace(request.Name))
            {
                wheres = wheres.And(s => s.Name.Contains(request.Name));
            }
            int total = 0;
            List<V_ProductRespones> lists = OperateContext.CurrentContext.BllSession.IV_ProductRespones_BLL
                .GetPageList(request.PageNumber, request.PageSize, out total, wheres, o => o.ProductID, true);

            string[] Codes = lists.Select(s => s.Code).ToArray();

            var Images_list = OperateContext.CurrentContext.BllSession.IProductPicture_BLL
                .GetListByWhere(s => Codes.Contains(s.Code));

            var list = lists.Select<V_ProductRespones, ProductRespones>(s =>
                       new ProductRespones()
                       {
                           Code = s.Code,
                           ProductID = s.ProductID,
                           Cost = s.Cost,
                           Name = s.Name,
                           ProductDesc = s.ProductDesc,
                           ProductPackage = s.ProductPackage,
                           Weight = s.Weight,
                           Images = Images_list.Where(o=>o.Code==s.Code).ToList()
                       });
             
            return OperateContext.CurrentContext.RedirectAjax(AjaxMsgStatuEnum.OK, "", new { rows = list, total = total });
        }
         
         
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.LanguageType = SystemController.LanguageTypeList;
            ViewBag.Category = SystemController.GetCategory();
            ViewBag.Contraband = SystemController.ContrabandList;
            ViewBag.ProductSpec = SystemController.ProductSpecList;
            ViewBag.ProductStatu = SystemController.ProductStatuList;
            ViewBag.ProductType = SystemController.ProductTypeList;
            ViewBag.Stock = SystemController.StockList;
            ViewBag.TransportType = SystemController.TransportTypeList;

            return View();
        }

        [HttpPost]
        [AjaxRequest]
        public ActionResult Add(AddProductRequest model)
        {
            
            if (!ModelState.IsValid)
            { 
                return OperateContext.CurrentContext.RedirectAjax(AjaxMsgStatuEnum.Error, "数据有错误！请标准填写");
            }
            else
            {
                int productCount = SystemController.AddProduct(new Product()
                  {
                      Code = model.Code,
                      CreateTime = DateTime.Now,
                      CreateUser = "AddProduct",
                      IsDel = false,
                      Name = model.Name,
                      ProductDesc = model.ProductDesc,
                      ProductPackage = model.ProductPackage,
                      UpdateTime = DateTime.Now,
                      UpdateUser = "Add"

                  });

                int productCategoryCount = SystemController.AddProductCategory(new ProductCategory()
                {
                    CategoryID = model.CategoryID,
                    Code = model.Code,
                    CreateTime = DateTime.Now,
                    CreateUser = "AddProduct",
                    IsDel = false,
                    UpdateTime = DateTime.Now,
                    UpdateUser = "Add"
                });

                int productConfigCount = SystemController.AddProductConfig(new ProductConfig()
                  {
                      Code = model.Code,
                      ContrabandIdList = string.Join(",", model.ContrabandIdList),
                      CreateTime = DateTime.Now,
                      CreateUser = "AddProduct",
                      DistributeAreaStockID = model.DistributeAreaStockID,
                      IsAdorn = model.IsAdorn,
                      IsDel = false,
                      ProductSpecID = model.ProductSpecID,
                      ProductStatusID = model.ProductStatusID,
                      ProductTypeID = model.ProductTypeID,
                      Remark = "",
                      TransportType = model.TransportType,
                      UpdateTime = DateTime.Now,
                      UpdateUser = "Add"
                  });

                int productCostCount = SystemController.AddProductCost(new ProductCost()
                  {
                      Code = model.Code,
                      Cost = model.Cost,
                      CreateTime = DateTime.Now,
                      CreateUser = "AddProduct",
                      IsDel = false,
                      UpdateTime = DateTime.Now,
                      UpdateUser = "Add",
                      Weight = model.Weight
                  });

                int productPackageInfoCount = SystemController.AddProductPackageInfo(new ProductPackageInfo()
                 {
                     Bulkfactor = model.Bulkfactor,
                     Code = model.Code,
                     CreateTime = DateTime.Now,
                     CreateUser = "AddProduct",
                     IsDel = false,
                     UpdateTime = DateTime.Now,
                     UpdateUser = "Add",
                     Girth = model.Girth,
                     Height = model.Height,
                     IsHighValue = model.IsHighValue,
                     Length = model.Length,
                     Volume = model.Volume,
                     Warehouse = model.Warehouse,
                     Weight = (decimal)model.Weight,
                     Width = model.Width,
                     Remark = ""
                 });

                SystemController.AddProductPicture(new ProductPicture()
                {

                });
                return OperateContext.CurrentContext.RedirectAjax(AjaxMsgStatuEnum.OK, "添加成功"); 
            }
            
        }
         

        [HttpGet]
        public ActionResult GetAmazonBrowseNode(int site)
        {
            List<View_AmazonBrowseNode> list = OperateContext.CurrentContext.BllSession.IView_AmazonBrowseNode_BLL.GetListByWhere(s => s.SiteId == site && s.BrowseNodePId == s.BrowseNodeRootId);
            return View("PartialAmazonCategory", list);
        }

        [HttpPost]
        public ActionResult GetAmazonBrowseNode(int site, string pid)
        {
            List<View_AmazonBrowseNode> list = OperateContext.CurrentContext.BllSession.IView_AmazonBrowseNode_BLL.GetListByWhere(s => s.SiteId == site && s.BrowseNodePId == pid);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
         
    }
}